package com.kantboot.system.user.service.impl;

import com.alibaba.fastjson2.JSON;
import com.kantboot.system.user.dao.repository.entity.SysPermissionRepository;
import com.kantboot.system.user.domain.dto.SysPermissionSearchDTO;
import com.kantboot.system.user.domain.entity.SysPermission;
import com.kantboot.system.user.service.ISysPermissionService;
import com.kantboot.system.user.service.IUserSelfService;
import com.kantboot.util.common.exception.BaseException;
import com.kantboot.util.core.param.PageParam;
import com.kantboot.util.core.redis.RedisUtil;
import com.kantboot.util.core.result.PageResult;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class SysPermissionServiceImpl implements ISysPermissionService {

    @Resource
    private SysPermissionRepository repository;

    @Resource
    private IUserSelfService userSelfService;

    @Resource
    private RedisUtil redisUtil;

    @Override
    public List<SysPermission> getByRoleCode(String roleCode) {
        return repository.getByRoleCode(roleCode);
    }

    /**
     * 根据用户ID获取用户权限
     */
    @Override
    public List<SysPermission> getByUserId(Long userId) {
        return repository.getByUserId(userId);
    }

    @Override
    public List<SysPermission> getSelf() {
        Long selfId = userSelfService.getSelfId();
        return getByUserId(selfId);
    }

    @Override
    public List<String> getUriListByUserId(Long userId) {
        return repository.getUriListByUserId(userId);
    }

    @Override
    public List<String> getSelfUriList() {
        Long selfId = null;
        try{
            selfId = userSelfService.getSelfId();
        }catch (BaseException e){
            return getNotNeedLoginUriList();
        }

        String redisKey = "user:permission:uri:" + selfId;
        if (redisUtil.hasKey(redisKey)) {
            return JSON.parseArray(redisUtil.get(redisKey), String.class);
        }
        List<String> uriList = getUriListByUserId(selfId);
        // 存储到redis中30分钟
        redisUtil.setEx(redisKey, JSON.toJSONString(uriList), 30, TimeUnit.MINUTES);
        return uriList;
    }

    @Override
    public List<String> getNotNeedLoginUriList() {
        String redisKey = "permission:uri:notNeedLoginList";
        if (redisUtil.hasKey(redisKey)) {
            return JSON.parseArray(redisUtil.get(redisKey), String.class);
        }
        List<String> uriListByTypeCode = repository.getUriListByTypeCode(SysPermission.TYPE_CODE_NOT_NEED_LOGIN);
        // 存储到redis中30分钟
        redisUtil.setEx(redisKey, JSON.toJSONString(uriListByTypeCode), 30, TimeUnit.MINUTES);
        return uriListByTypeCode;
    }

    @Override
    public List<String> getCodeListByUserId(Long userId) {
        return repository.getCodeListByUserId(userId);
    }

    @Override
    public List<String> getSelfCodeList() {
        Long selfId = userSelfService.getSelfId();
        return getCodeListByUserId(selfId);
    }

    @Override
    public PageResult getBodyData(PageParam<SysPermissionSearchDTO> pageParam) {
        return PageResult.of(repository.getBodyData(pageParam.getData(),pageParam.getPageable()));
    }

    @Override
    public List<SysPermission> getListByCodeList(List<String> codeList) {
        return repository.getListByCodeList(codeList);
    }
}
